package com.myPlaneGame.obj;

import com.myPlaneGame.GameCreate;
import com.myPlaneGame.Utils.GameUtils;

import java.awt.*;

public class Enemy2Obj extends GameObj{
    //设置血量属性
    int health=6;

    public Enemy2Obj() {
        super();
    }

    public Enemy2Obj(Image img, int width, int height, int x, int y, double speed, GameCreate frame) {
        super(img, width, height, x, y, speed, frame);
    }

    public Enemy2Obj(Image img, int x, int y, double speed) {
        super(img, x, y, speed);
    }

    @Override
    public void paintSelf(Graphics g) {
        super.paintSelf(g);
        y+=speed;
        //碰撞检测
        //1.我方飞机一级子弹和敌方大飞机碰撞(5次击杀)
        for(ShellObj shellObj: GameUtils.shellObjList){
            if(this.getRec().intersects(shellObj.getRec())&&health>0){
                shellObj.setX(-100);
                shellObj.setY(-100);
                GameUtils.removeList.add(shellObj);
                health--;
            }else if(this.getRec().intersects(shellObj.getRec())){
                //碰撞之后出现爆炸动画
                ExplodeObj explodeObj=new ExplodeObj(x,y);
                GameUtils.explodeObjList.add(explodeObj);
                GameUtils.removeList.add(explodeObj);

                shellObj.setX(-100);
                shellObj.setY(-100);
                GameUtils.removeList.add(shellObj);
                this.x=-200;
                this.y=-200;
                GameUtils.removeList.add(this);
                GameCreate.score+=2;
            }
        }
        //2.我方飞机二级子弹和敌方大飞机碰撞
        for(DoubleShellObj doubleShellObj: GameUtils.doubleShellObjList){
            if(this.getRec().intersects(doubleShellObj.getRec())&&health>0){
                doubleShellObj.setX(-100);
                doubleShellObj.setY(-100);
                GameUtils.removeList.add(doubleShellObj);
                health-=2;
            }else if(this.getRec().intersects(doubleShellObj.getRec())){
                //碰撞之后出现爆炸动画
                ExplodeObj explodeObj=new ExplodeObj(x,y);
                GameUtils.explodeObjList.add(explodeObj);
                GameUtils.removeList.add(explodeObj);

                doubleShellObj.setX(-100);
                doubleShellObj.setY(-100);
                GameUtils.removeList.add(doubleShellObj);
                this.x=-200;
                this.y=-200;
                GameUtils.removeList.add(this);
                GameCreate.score+=2;
            }
        }
        //3.我方飞机三级子弹和敌方大飞机碰撞
        for(TripleShellObj tripleShellObj: GameUtils.tripleShellObjList){
            if(this.getRec().intersects(tripleShellObj.getRec())&&health>0){
                tripleShellObj.setX(-100);
                tripleShellObj.setY(-100);
                GameUtils.removeList.add(tripleShellObj);
                health-=3;
            }else if(this.getRec().intersects(tripleShellObj.getRec())){
                //碰撞之后出现爆炸动画
                ExplodeObj explodeObj=new ExplodeObj(x,y);
                GameUtils.explodeObjList.add(explodeObj);
                GameUtils.removeList.add(explodeObj);

                tripleShellObj.setX(-100);
                tripleShellObj.setY(-100);
                GameUtils.removeList.add(tripleShellObj);
                this.x=-200;
                this.y=-200;
                GameUtils.removeList.add(this);
                GameCreate.score+=2;
            }
        }
        //技能开启保护网
        if(PlaneObj.skillUsing==2){
            if (this.y>450&&this.y<800) {
                this.x=-1000;
                this.y=-1000;
                GameUtils.removeList.add(this);
            }
        }
        //越界判断：清除跃出游戏窗口的数据（降低游戏页面绘制的压力）
        if(this.y>800){
            this.x=-1000;
            this.y=-1000;
            GameUtils.removeList.add(this);
        }
        //重新刷新游戏数据
        if(GameCreate.state==3||GameCreate.state==4){
            this.x=-1000;
            this.y=-1000;
            GameUtils.removeList.add(this);
        }

    }

    @Override
    public Rectangle getRec() {
        return super.getRec();
    }
}
